package com.slipper.system.codeless.controller;

import com.slipper.core.tree.TreeInfo;
import com.slipper.core.tree.TreeUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.slipper.system.codeless.bo.AppBO;
import com.slipper.system.codeless.service.AppService;
import com.slipper.constants.Constants;
import com.slipper.util.ResultUtil;
import com.slipper.util.Tool;

import java.util.List;

@RestController
@RequestMapping("/system/app")
public class AppController {
    @Resource
    private AppService appService;
    
    @PostMapping(value = "/queryBy")
    public String queryBy(@RequestBody AppBO bo) {
        if(Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("数据获取失败！");
        }
        return ResultUtil.successResult(appService.queryBy(bo.getId()));
    }

    @PostMapping(value = "/queryList")
    public String queryList(@RequestBody AppBO bo) {
        return ResultUtil.successResult(appService.queryList(bo));
    }
    
    @PostMapping(value = "/queryPage")
    public String queryPage(@RequestBody AppBO bo) {
        return ResultUtil.successResult(appService.queryPage(bo));
    }

    @PostMapping(value = "/queryTree")
    public String queryTree() {
        List<AppBO> folderList = appService.queryList(new AppBO());
        TreeInfo treeInfo = new TreeInfo("应用程序");
        treeInfo.setRootId(Constants.ROOT_ID);
        treeInfo.setIdField("id");
        treeInfo.setPidField(null);
        treeInfo.setTextField("name");
        return ResultUtil.successResult(TreeUtil.buildTree(treeInfo, folderList, "asc"));
    }

    @PostMapping(value = "/save")
    public String save(@RequestBody AppBO bo) {
        bo.setOperateId(Constants.getCurrentUser().getEmployeeId());
        return ResultUtil.saveResult(appService.save(bo), bo);
    }

    @PostMapping(value = "/delete")
    public String delete(@RequestBody AppBO bo) {
        return ResultUtil.deleteResult(appService.delete(bo));
    }
}
